<?php
/**
 * Concord CRM - https://www.concordcrm.com
 *
 * @version   1.3.4
 *
 * @link      Releases - https://www.concordcrm.com/releases
 * @link      Terms Of Service - https://www.concordcrm.com/terms
 *
 * @copyright Copyright (c) 2022-2023 KONKORD DIGITAL
 */

namespace Modules\Core\Support\Macros\Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ListIndexes
{
    /**
     * List indexes from the given table or model, optionally provide a column.
     */
    public function __invoke(string|Model $table, string $column = null): array
    {
        $tableName = $table instanceof Model ? $table->getTable() : $table;
        $prefix = $table instanceof Model ? $table->getConnection()->getTablePrefix() : DB::getTablePrefix();

        $query = 'SHOW KEYS FROM '.$prefix.$tableName.'';

        if ($column) {
            $query .= ' WHERE Column_name=\''.$column.'\'';
        }

        return DB::select(DB::raw($query)->getValue(DB::connection()->getQueryGrammar()));
    }
}
